# encoding: utf-8
"""
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    created by lane.chang on '17/10/2024'
    comment: 服务流程
"""
from fastapi import APIRouter, Request, Query

from config import Config
from project.core.function import json_response
from project.lib.decorator import api_common, exec_duration
from project.service.flow import FlowService
from project.service.cariabot import CariabotService
from project.middleware.homecare import HomeCare


flow_router = APIRouter(prefix='/c', tags=['cariabot.flow'])


@flow_router.get('/flow/list', summary='流程列表')
@api_common(doc_replace=(('{0}', Config.ENV_DOC_FIX),), dev_info=True)
@exec_duration(add_return=True)
async def get_flow_list(request: Request,
                        sidebar_id: int = Query(..., title='侧边栏id'),
                        session=None,
                        device=None):
    """
    **desc**: 服务流程列表

    **url**: http(s)://{0}.udicaria.com/c/flow/list

    **method**: GET

    **params**:

        <query>:
            sidebar_id: 侧边栏id

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data: [
                {
                    code: 服务流程编号
                    name: 服务流程名称
                    image: 服务流程图片
                }
            ]
            exec_time: 接口后端处理时长
        }
    """
    flows = await FlowService.get_cariabot_flow_list(session, device, sidebar_id=sidebar_id)

    return json_response(data=[v.dict('code', 'name', 'image', 'hint') for v in flows])


@flow_router.get('/external/list', summary='cariabot外部模块列表')
@api_common(doc_replace=(('{0}', Config.ENV_DOC_FIX),), dev_info=True)
@exec_duration(add_return=True)
async def get_external_list(request: Request,
                            sidebar_id: int = Query(..., title='侧边栏id'),
                            session=None,
                            device=None):
    """
    **desc**: cariabot外部模块列表

    **url**: http(s)://{0}.udicaria.com/c/external/list

    **method**: GET

    **params**:

        <query>:
            sidebar_id: 侧边栏id

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data: [
                {
                    code: 服务流程编号
                    name: 服务流程名称
                    image: 服务流程图片
                }
            ]
            exec_time: 接口后端处理时长
        }
    """
    data = await CariabotService.get_cariabot_external_list(session, device, sidebar_id=sidebar_id)

    return json_response(data=[v.dict() for v in data])


@flow_router.get('/flow/scheduling/day', summary='取得排班日期')
@api_common(doc_replace=(('{0}', Config.ENV_DOC_FIX),), dev_info=True)
@exec_duration(add_return=True)
async def get_scheduling_day(request: Request,
                             flow_code: str = Query('', title='流程编号'),
                             month: str = Query('...', title='月份'),
                             device=None):
    """
    **desc**: 取得排班日期

    **url**: http(s)://{0}.udicaria.com/c/flow/scheduling/day

    **method**: POST

    **params**:

        <body>:
            flow_code: 流程编号
            month: 月份(%Y-%m)

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data: [
                {
                    date: 日期
                    status: 可用状态（1:未约满；0:已约满）
                }
            ]
            exec_time: 接口后端处理时长
        }
    """
    if not Config.NURSING_SERVICE_MAP.get(flow_code):
        json_response(data=[])

    ret_data = await HomeCare.get_scheduling_day(device_id=device.dev_id,
                                                 month=month,
                                                 service_name=Config.NURSING_SERVICE_MAP.get(flow_code))

    return json_response(data=ret_data)


@flow_router.get('/flow/scheduling/time_period', summary='取得排班时段')
@api_common(doc_replace=(('{0}', Config.ENV_DOC_FIX),), dev_info=True)
@exec_duration(add_return=True)
async def get_scheduling_time_period(request: Request,
                                     flow_code: str = Query('', title='流程编号'),
                                     day: str = Query('...', title='服务日期'),
                                     device=None):
    """
    **desc**: 取得排班时段

    **url**: http(s)://{0}.udicaria.com/c/flow/scheduling/time_period

    **method**: POST

    **params**:

        <body>:
            flow_code: 流程编号
            day: 服务日期(%Y-%m-%d)

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data: [
                {
                    time: 时段
                    number: 预留量
                    status: 可用状态（1:未约满；0:已约满）
                }
            ]
            exec_time: 接口后端处理时长
        }
    """
    if not Config.NURSING_SERVICE_MAP.get(flow_code):
        json_response(data=[])

    ret_data = await HomeCare.get_scheduling_time_period(device_id=device.dev_id,
                                                         start_day=day,
                                                         end_day=day,
                                                         service_name=Config.NURSING_SERVICE_MAP.get(flow_code))
    return json_response(data=ret_data)
